package com.itsu.spbspider.service.impl;

import com.itsu.spbspider.exception.SystemException;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author suben
 * @create time 2020/2/10 19:53
 */
@Service("baiduWeatherCrawlerWithCity")
public class BaiduWeatherCrawlerWithCity extends BaiduWeatherCrawler {

    private String city;

    private AtomicInteger count = new AtomicInteger(0);

    public String crawlerWeatherData(String city) throws Exception {
        try {
            this.city = city;
            return super.crawlerData(10);
        } catch (Exception e) {
            throw new SystemException(e);
        }
    }

    @Override
    protected Map<String, String> initHeadersParams(String content) throws IllegalAccessException {
        Map<String, String> result = super.initHeadersParams(content);
        if (count.intValue() == 2) {
            count = new AtomicInteger(0);
        }
        if (this.count.intValue() == 0) {
            result.put("city", this.city);
        }
        count.addAndGet(1);
        return result;
    }
}
